import os.path
import time

import allure

from sample.common.base_page import BasePage
from faker import Faker
from config.sys_path import DATAS_DIR

fk = Faker('zh_CN')


def get_add_buyers_test_data(start_tel, count) -> list[dict]:
    tel = start_tel
    lists = []
    for i in range(0, count):
        time.sleep(0.01)
        time_stamp = str(time.time_ns())
        data = {'buyersName': '买家' + time_stamp,
                'socialCreditCode': time_stamp,
                'entContactName': 'D先生UI',
                'contactMobile': tel,
                'businessAddress': 'UI公司详细地址',
                'receiverAddr': 'UI收货详细地址',
                'receiverName': 'D收货人UI',
                'receiverMobile': tel
                }
        lists.append(data)
        print(f'创建用户：loginId:【{tel}】')
        tel += 1
    return lists


class RegistrationInformation(BasePage):
    URL = '/#/platformHome/index'

    @allure.step('导航到客户管理-注册信息页面')
    def switch_to_page(self):
        self.navigate(self.URL)
        menu = self.get_locator('//ul[@role="menubar"]/div/li[div/span[text()="客户管理"]]')
        menu.click()
        page = menu.locator('//ul//a[li/span[text()="注册信息"]]')
        page.click()

    @allure.step('输入买家注册信息')
    def fill_buyers_information(self, datas: dict):
        self.fill_input('RegistrationInformation.BuyersNameInput', datas['buyersName'])
        self.fill_input('RegistrationInformation.SocialCreditCodeInput', datas['socialCreditCode'])
        self.fill_input('RegistrationInformation.EntContactNameInput', datas['entContactName'])
        self.fill_input('RegistrationInformation.ContactMobileInput', datas['contactMobile'])
        self.select_option('RegistrationInformation.BusinessAddressInput', '湖南省', '长沙市', '开福区')
        self.fill_input('RegistrationInformation.BusinessDetailAddressInput', datas['businessAddress'])
        self.select_option('RegistrationInformation.ReceiverAddrInput', '湖南省', '长沙市', '岳麓区')
        self.fill_input('RegistrationInformation.ReceiverDetailAddrInput', datas['receiverAddr'])
        self.fill_input('RegistrationInformation.ReceiverNameInput', datas['receiverName'])
        self.fill_input('RegistrationInformation.ReceiverMobileInput', datas['receiverMobile'])

    @allure.step('上传买家资质文件')
    def upload_certification(self):
        self.click_element('RegistrationInformation.Tab2')
        self.set_checked('RegistrationInformation.ChainDrugstoresRadio', True)
        upload_list = self.get_locators('RegistrationInformation.CertificationUploadList')
        # 目前所有资质全上传同一张图片，后续可改为根据资质名称传不同的文件
        for i in upload_list:
            i.locator('..').hover()  # 上传文件之前，将鼠标移动到对应位置
            i.set_input_files(os.path.join(DATAS_DIR, 'images', 'testUpload.png'))
            time.sleep(1.5)
        self.click_element('RegistrationInformation.ConfirmBtn')
        self.click_element('RegistrationInformation.CloseDialogBtn')

    @allure.step('审核指定的注册信息')
    def approved_buyer_registration_information(self, buyers_name, contact_mobile):
        self.page.reload()
        self.click_element('RegistrationInformation.RefreshBtn')
        self.save_screenshot_allure('待审核截图')
        self.click_element(f'RegistrationInformation.AuditBtn,{buyers_name},{contact_mobile}')
        self.click_element('RegistrationInformation.BusinessScopeTab')
        checkbox_list = self.get_locators('RegistrationInformation.BusinessScopeCheckboxList')
        for checkbox in checkbox_list:
            checkbox.click()
        self.click_element('RegistrationInformation.DeliveryAddressTab')
        self.click_element('RegistrationInformation.EditBtn')
        self.fill_input('RegistrationInformation.CompanyNameInput', 'UI随货人test')
        self.click_element('RegistrationInformation.ConfirmBtn')
        self.click_element('RegistrationInformation.InvoiceInformationTab')
        self.fill_input('RegistrationInformation.InvoiceNameInput', '发票名称' + fk.pystr()[:10])
        self.fill_input('RegistrationInformation.InvoiceTaxInput', 'FPSH' + fk.pystr()[:10])
        self.click_element('RegistrationInformation.QualificationTab')
        document_list = self.get_locators('RegistrationInformation.DocumentInputList')
        for document in document_list:
            document.fill(fk.pystr())
        self.click_element('RegistrationInformation.ApprovedBtn')
        # 检查是否有资质文件未上传，并补充完整
        while self.is_element_exist('RegistrationInformation.ClickUpload'):
            self.click_element('RegistrationInformation.ClickUpload')
            self.page.wait_for_timeout(1000)
            self.click_element('RegistrationInformation.FirstImage')
            self.click_element('RegistrationInformation.ConfirmBtn')
            self.click_element('RegistrationInformation.ApprovedBtn')
